

import worker from '@ohos.worker'
import filemanager from '@ohos.fileManager'


var TAG = 'DataWorker'
const parentPort = worker.parentPort
parentPort.onmessage = function (e) {
    let data = e.data
    if (data.request_data == 'getRoot') {
        getRootData(data)
    } else if (data.request_data == 'listFile') {
        getListFileData(data)
    }
}

parentPort.onmessageerror = function () {
    console.log('aaa-->onmessageerror')
}

parentPort.onerror = function (data) {
    console.log('aaa-->onerror')
}

function getRootData(data) {
    filemanager.getRoot()
        .then(file => {
            handleData(file, data)

        })
        .catch((error) => {
            console.log('aaa--getRoot 2 error' + error)
        });
}

function getListFileData(data) {

    if (data.media_type == 'file') {
        filemanager.listFile(data.path, data.media_type)
            .then(fileFolder => {
                if(Array.isArray(fileFolder)) {
                    for (var i = 0; i < fileFolder.length; i++) {
                        var folder = fileFolder[i]
                        filemanager.listFile(folder.path, 'file')
                            .then(fileList => {
                                handleData(fileList, data)
                            })
                            .catch((error) => {
                                console.log('aaa--folderError' + error)

                            })
                    }
                }

            })
            .catch((error) => {
                console.log('aaa--getFolderDataError' + error)
            })
    }
    if (data.media_type == 'video') {
        filemanager.listFile(data.path, data.media_type)
            .then(fileFolder => {
                if(Array.isArray(fileFolder)) {
                    for (var i = 0; i < fileFolder.length; i++) {
                        var folder = fileFolder[i]
                        filemanager.listFile(folder.path, 'video')
                            .then(fileList => {
                                handleData(fileList, data)
                            })
                            .catch((error) => {
                                console.log('aaa--folderError' + error)
                            })
                    }
                }

            })
            .catch((error) => {
                console.log('aaa--getFolderDataError' + error)
            })
    } if (data.media_type == 'audio') {
        filemanager.listFile(data.path, data.media_type)
            .then(fileFolder => {
                if(Array.isArray(fileFolder)) {
                    for (var i = 0; i < fileFolder.length; i++) {
                        var folder = fileFolder[i]
                        filemanager.listFile(folder.path, 'audio')
                            .then(fileList => {
                                handleData(fileList, data)
                            })
                            .catch((error) => {
                                console.log('aaa--folderError' + error)

                            })
                    }
                }

            })
            .catch((error) => {
                console.log('aaa--getFolderDataError' + error)
            })
    }else {
        filemanager.listFile(data.path, 'file')
            .then(fileFolder => {
                if(Array.isArray(fileFolder)) {
                    for (var i = 0; i < fileFolder.length; i++) {
                        var folder = fileFolder[i]
                        filemanager.listFile(folder.path, 'file')
                            .then(fileList => {
                                if(Array.isArray(fileList)){
                                    for (var index = 0; index < fileList.length; index++) {
                                        var file = fileList[index]
                                        filemanager.listFile(data.path+file.path, data.media_type)
                                            .then(fileItem => {
                                                handleData(fileItem, data)
                                            })
                                            .catch((error) => {
                                                console.log('aaa--fileListError' + error)
                                            })
                                    }
                                }
                            })
                            .catch((error) => {
                                console.log('aaa--fileError' + error)
                            })
                    }
                }
            })
            .catch((error) => {
                console.log('aaa--getFileError' + error)
            })
    }
}

function handleData(file, data) {

    var info = JSON.stringify(file)
    console.log('hans------.,.,.>>>>'+info)
    var buf = new ArrayBuffer(info.length * 2)
    var bufView = new Uint16Array(buf)
    for (var index = 0; index < info.length; index++) {
        bufView[index] = info.charCodeAt(index)
    }
    parentPort.postMessage({ data: bufView, params: data })
}

